package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.CustomerLocationRpcService;
import com.hunttown.mes.rpc.domain.CustomerLocationDTO;
import com.hunttown.mes.rpc.domain.query.CustomerLocationDTOQuery;
import com.hunttown.mes.service.CustomerLocationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.CustomerLocationConverts.convert;

/**
 * created by wangjunfu on 2021-06-08 14:12:19
 */
@Service
public class CustomerLocationRpcServiceImpl implements CustomerLocationRpcService {

    private final Logger logger = LoggerFactory.getLogger(CustomerLocationRpcServiceImpl.class);

    private final CustomerLocationService customerLocationService;

    @Autowired
    public CustomerLocationRpcServiceImpl(CustomerLocationService customerLocationService) {
        this.customerLocationService = customerLocationService;
    }

    @Override
    public CustomerLocationDTO insert(CustomerLocationDTO customerLocationDTO) {
        try {
            return convert(customerLocationService.insert(convert(customerLocationDTO)));
        } catch (Exception e) {
            logger.error("CustomerLocation create failed.", e);
            throw new RuntimeException("CustomerLocation create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(CustomerLocationDTO customerLocationDTO) {
        try {
            return customerLocationService.updateInfoById(convert(customerLocationDTO));
        } catch (Exception e) {
            logger.error("CustomerLocation update by id failed.", e);
            throw new RuntimeException("CustomerLocation update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(CustomerLocationDTOQuery customerLocationDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (customerLocationDTOQuery.getW_id() != null && customerLocationDTOQuery.getW_id() > 0) {
                query.put("w_id", customerLocationDTOQuery.getW_id());
            }

            if (customerLocationDTOQuery.getIds() != null && !customerLocationDTOQuery.getIds().equals("")) {
                query.put("ids", customerLocationDTOQuery.getIds());
            }

            if (customerLocationDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", customerLocationDTOQuery.getStopFlag());
            }

            if (customerLocationDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", customerLocationDTOQuery.getDeleteFlag());
            }

            return customerLocationService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("CustomerLocation update by query failed.", e);
            throw new RuntimeException("CustomerLocation update by query failed, Please order your code.", e);
        }
    }

    @Override
    public CustomerLocationDTO selectById(Integer id) {
        try {
            return convert(customerLocationService.selectById(id));
        } catch (Exception e) {
            logger.error("CustomerLocation select by id failed.", e);
            throw new RuntimeException("CustomerLocation select by id failed, Please order your code.", e);
        }
    }

    @Override
    public CustomerLocationDTO selectObjByQuery(CustomerLocationDTOQuery customerLocationDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (customerLocationDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", customerLocationDTOQuery.getStopFlag());
            }

            if (customerLocationDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", customerLocationDTOQuery.getDeleteFlag());
            }

            if (StringUtils.isNotBlank(customerLocationDTOQuery.getOrderBy())) {
                query.put("orderBy", customerLocationDTOQuery.getOrderBy());
            }

            return convert(customerLocationService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("CustomerLocation select obj by query failed.", e);
            throw new RuntimeException("CustomerLocation select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<CustomerLocationDTO> getListForPage(CustomerLocationDTOQuery customerLocationDTOQuery) {
        Query query = Query.page(customerLocationDTOQuery.getPn(), customerLocationDTOQuery.getPageSize());

        if (customerLocationDTOQuery.getId() != null && customerLocationDTOQuery.getId() > 0) {
            query.put("id", customerLocationDTOQuery.getId());
        }

        if (customerLocationDTOQuery.getIds() != null && !customerLocationDTOQuery.getIds().equals("")) {
            query.put("ids", customerLocationDTOQuery.getIds());
        }

        if (StringUtils.isNotBlank(customerLocationDTOQuery.getLikeName())) {
            query.put("likeName", customerLocationDTOQuery.getLikeName());
        }

        if (customerLocationDTOQuery.getTableId() != null) {
            query.put("tableId", customerLocationDTOQuery.getTableId());
        }

        if (customerLocationDTOQuery.getRecordId() != null) {
            query.put("recordId", customerLocationDTOQuery.getRecordId());
        }

        if (customerLocationDTOQuery.getUserType() != null) {
            query.put("userType", customerLocationDTOQuery.getUserType());
        }

        if (customerLocationDTOQuery.getUserId() != null) {
            query.put("userId", customerLocationDTOQuery.getUserId());
        }

        if (customerLocationDTOQuery.getLocationType() != null) {
            query.put("locationType", customerLocationDTOQuery.getLocationType());
        }

        if (StringUtils.isNotBlank(customerLocationDTOQuery.getShowDate())) {
            query.put("showDate", customerLocationDTOQuery.getShowDate());
        }

        if (StringUtils.isNotBlank(customerLocationDTOQuery.getStartDate())) {
            query.put("startDate", customerLocationDTOQuery.getStartDate());
        }

        if (StringUtils.isNotBlank(customerLocationDTOQuery.getEndDate())) {
            query.put("endDate", customerLocationDTOQuery.getEndDate());
        }

        if (customerLocationDTOQuery.getStopFlag() != null) {
            query.put("stopFlag", customerLocationDTOQuery.getStopFlag());
        }

        if (customerLocationDTOQuery.getDeleteFlag() != null) {
            query.put("deleteFlag", customerLocationDTOQuery.getDeleteFlag());
        }

        if (StringUtils.isNotBlank(customerLocationDTOQuery.getOrderBy())) {
            query.put("orderBy", customerLocationDTOQuery.getOrderBy());
        }

        List<CustomerLocationDTO> list = convert(customerLocationService.selectByQuery(query));
        Page<CustomerLocationDTO> page = Page.from(customerLocationDTOQuery.getPn(), customerLocationDTOQuery.getPageSize(), list);
        page.setTotalItems(customerLocationService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return customerLocationService.deleteById(id);
        } catch (Exception e) {
            logger.error("CustomerLocation delete failed.", e);
            throw new RuntimeException("CustomerLocation delete failed, Please order your code.", e);
        }
    }
}